<template>
  <div>
    <div id="page">
      <!-- 头部 -->
      <header>
        <div class="top">
          <!-- <span class="iconfont">&#xe6bc;</span> -->
          <router-link to="/index/cart">&lt;</router-link>
          <h2>确认订单</h2>
          <p class="top">
            <span class="iconfont">&#xf0112;</span>
            <span class="iconfont">&#xe71e;</span>
            <i></i>
          </p>
        </div>
        <div class="massage" @click="add()">
          <h2>{{address.username}} {{address.userphone}}</h2>
          <p>{{address.location}} {{address.useraddress}}</p>
        </div>
      </header>
      <!-- 主体 -->
      <main>
        <!-- 品牌精选 -->
        <p id="store"><img src="../../assets/images/order-1.png" alt=""><span>品牌精选</span><em>已免运费</em><b></b><a href="#">领券</a>
        </p>
        <ul class="sift">
          <li v-for="item in cartList" :key="item.id">
            <img :src="$pre+item.img" alt="">
            <div class="commodity">
              <h3>{{item.goodsname}}</h3>
              <p>{{item.goodsname}}</p>
              <p class="price"><em>￥</em><i>{{item.price}}</i><b>x{{item.num}}</b></p>
              <p class="limit"><span>七天无理由退货</span><em>特价</em></p>
            </div>
          </li>
          <!-- <li>
            <img src="../../assets/images/shop_bg1.png" alt="">
            <div class="commodity">
              <h3>多芬男士+护理净油洁面泡...</h3>
              <p>击退油光,净在掌握,有效控油...</p>
              <p class="price"><em>￥</em><i>999</i><b>x1</b></p>
              <p class="limit"><span>七天无理由退货</span><em>特价</em></p>
            </div>
          </li> -->
        </ul>
        <div class="center">
          <p>
            <i>发票类型</i> <span>不开发票></span></p>
          <p><i>售后免邮</i><span>部分商家赠送</span></p>
          <p><i>买家留言</i><span>填写内容需要与商家协商并确认，45字以内</span></p>
        </div>
        <div class="bottom">
          <p><i>商品金额</i> <span>￥1998></span></p>
          <p><i>优惠活动</i><span>-￥200</span></p>
          <p><i>优惠券</i><span>暂无可用优惠券</span></p>
        </div>

      </main>
      <!-- 底部 -->
      <footer>
        <div class="left">
          <span><em>合计{{num}}件</em>已免运费</span>
          <span><i>￥{{money}}</i>已优惠200元</span>
        </div>
        <a href="#" @click.prevent="submit">确认订单</a>
      </footer>
    </div>
  </div>
</template>

<script>
import {addressList,cartList,orderAdd} from "../../request/api"
import { Toast } from 'vant';

  export default {
    data(){
      return{
        userInfo:sessionStorage.getItem("token")?JSON.parse(sessionStorage.getItem("token")):{},
        address:[],
        cartList:[]
      }
    },
    methods:{
      add(){
         this.$router.push("/address")
      },
      submit(){
        let idstr = this.$route.params.id.split(",")
        idstr= JSON.stringify(idstr.join());
        console.log(this.userInfo.uid,this.money,this.num,this.address.id,idstr);
        orderAdd({uid:this.userInfo.uid,countmoney:this.money,countnumber:this.num,addressid:this.address.id,idstr:idstr}).then(res=>{
          if(res.data.code==200){
            Toast(res.data.msg)
            this.$router.push("/mineOrder")
          }else{
            Toast(res.data.msg)
          }
        })
      }
    },
    computed:{
      money(){
        let money = 0
        this.cartList.map(item=>{
          return  money+=item.price*item.num
        })
        return money
      },
      num(){
        let num =0
          this.cartList.map(item=>{
          return  num+=item.num
        })
        return num
      }
    },

mounted(){
  this.cartList=[]
  // console.log(this.$route.params.id);

  // 从地址页传来的  渲染订单页的 地址
  addressList({uid:this.userInfo.uid,id:this.$route.query.id}).then(res=>{
    let address = res.data.list.filter(item=>{
      return item.status==1
    })
    this.address = address[0]
  });


  // 从购物车页面传来的 被选中商品的id值
  //  let id = this.$route.params.id.split(",");
  // //根据id渲染 商品
  //     cartList({uid:this.userInfo.uid}).then(res=>{
  //       res.data.list.forEach(item=>{
  //         id.forEach(item2=>{
  //           if(item2==item.id){
  //             this.cartList.push(item)
  //           }
  //         })
  //       })
  //     })

  //存在本地的 商品
  let goods = JSON.parse(sessionStorage.getItem("goods"));
  goods.forEach(item => {
    this.cartList.push(item)
  });

}
  }
</script>

<style scoped>
html,
body,
#page {
  height: 100%;
}
#page {
  max-width: 7.5rem;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}
header {
  background: #ff6040;
  height: 2.45rem;
  margin-bottom: 0.9rem;
}
header .top {
  height: 0.8rem;
  display: flex;
  color: #ffdfd8;
  align-items: center;
  padding: 0.3rem 0.12rem 0.12rem 0.33rem;
  box-sizing: border-box;
}
header .top h2 {
  font-size: 0.3rem;
  margin: 0 1.2rem 0 2.5rem;
}
header .top p {
  width: 1.72rem;
  height: 0.62rem;
  border: 1px solid #ff876c;
  border-radius: 0.31rem;
  line-height: 0.62rem;
  display: flex;
  justify-content: space-between;
  position: relative;
}
header .top p span {
  width: 0.86rem;
  text-align: center;
}
header .top p i {
  width: 1px;
  height: 0.37rem;
  background: #ff876c;
  position: absolute;
  left: 0.86rem;
  top: 0.12rem;
}
header .massage {
  width: 94%;
  margin: 0 auto;
  background: #fff;
  margin-top: 0.3rem;
  height: 2.1rem;
  border-radius: 0.1rem;
  padding: 0.4rem 0.5rem 0 0.4rem;
  box-sizing: border-box;
  color: #666666;
  font-size: 0.26rem;
}
header .massage h2 {
  font-size: 0.32rem;
  font-weight: bold;
  color: #333333;
  margin-bottom: 0.28rem;
}
main {
  margin-top: 0.2rem;
  flex: 1;
  overflow: auto;
}
main #store {
  background: #fff;
  padding: 0 0.2rem;
  display: flex;
  align-items: center;
  padding: 0.4rem 0.2rem;
  box-sizing: border-box;
}
main #store img {
  width: 0.32rem;
  margin-right: 0.2rem;
}
main #store span {
  font-size: 0.3rem;
  color: #333333;
  font-weight: bold;
  margin-right: 3.2rem;
}
main #store em {
  font-size: 0.22rem;
  color: #333333;
}
main #store b {
  width: 0.02rem;
  height: 0.22rem;
  background: #979797;
  margin: 0 0.21rem;
}
main #store a {
  color: #ff6c4e;
  font-size: 0.22rem;
}
main .sift {
  background: #fff;
}
main .sift li {
  height: 2.6rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.2rem 0.2rem;
}
main .sift li img {
  width: 2.6rem;
  margin-right: 0.25rem;
}
main .sift li .commodity {
  height: 100%;
}
main .sift li .commodity h3 {
  color: #333333;
  font-size: 0.3rem;
}
main .sift li .commodity p {
  color: #999999;
}
main .sift li .commodity .price {
  margin: 0.4rem 0 0.26rem;
}
main .sift li .commodity .price em {
  font-size: 0.14rem;
  color: #ff6040;
}
main .sift li .commodity .price i {
  color: #ff6040;
  font-size: 0.3rem;
}
main .sift li .commodity .price b {
  color: #333333;
  font-size: 0.26rem;
  margin-left: 2.7rem;
}
main .sift li .commodity .limit {
  display: flex;
  font-size: 0.18rem;
}
main .sift li .commodity .limit span {
  padding-left: 0.07rem;
  width: 2.5rem;
  height: 0.33rem;
  background: #ff8066;
  line-height: 0.33rem;
  color: #fff;
  box-sizing: border-box;
  margin-right: 0.1rem;
}
main .sift li .commodity .limit em {
  background: #ff8066;
  color: #fff;
  height: 0.33rem;
  line-height: 0.33rem;
}
main .center {
  padding: 0.22rem 0.2rem 0;
  background: #fff;
  margin-bottom: 0.2rem;
}
main .center p {
  display: flex;
  justify-content: space-between;
  font-size: 0.22rem;
  color: #666666;
  height: 0.64rem;
  line-height: 0.64rem;
}
main .center p i {
  color: #333333;
  font-weight: bold;
}
main .bottom {
  padding: 0 0.2rem;
  background: #fff;
}
main .bottom p {
  display: flex;
  justify-content: space-between;
  font-size: 0.22rem;
  color: #666666;
  height: 0.64rem;
  line-height: 0.64rem;
}
main .bottom p i {
  color: #333333;
  font-weight: bold;
}
main .bottom p:nth-child(2) span {
  color: #ff6c4e;
}
footer {
  display: flex;
  /* height: 1.66rem; */
  background: #fff;
  padding-left: 0.23rem;
  justify-content: space-between;
  border-top: 1px solid #cccccc;
      position: fixed;
    bottom: 0;
    left: 0;
    width: 100%
}
footer .left {
  display: flex;
}
footer .left span:nth-child(1) {
  margin-right: 0.36rem;
}
footer .left span {
  display: flex;
  flex-direction: column;
  font-size: 0.18rem;
  color: #9b9b9b;
}
footer .left span em {
  color: #333333;
  font-weight: bold;
  font-size: 0.3rem;
}
footer .left span i {
  font-size: 0.3rem;
  color: #ff6040;
  font-weight: bold;
}
footer a {
  color: #fff;
  background: #ff6040;
  font-size: 0.3rem;
  width: 2.2rem;
  height: 0.98rem;
  font-weight: bold;
  text-align: center;
  line-height: 0.98rem;
}

</style>